/**
 * 
 */
package com.cqan.account.controller;

import static com.cqan.common.ViewName.*;

import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.cqan.account.entity.Authority;
import com.cqan.account.entity.Role;
import com.cqan.account.service.AuthorityService;
import com.cqan.account.service.RoleService;
import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.orm.PropertyFilter;
import com.google.common.collect.Lists;

/**
 * @Description TODO
 * @author slaton.wu@gmail.com
 * @date 2012-6-29下午7:37:36
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController<Role> {
	
	@Resource(name="roleService")
	private RoleService roleService;
	
	@Resource(name="authorityService")
	private AuthorityService authorityService;
	
	@RequestMapping(value="/list.htm")
	public ModelAndView list(Integer pageNo,HttpServletRequest request){
		ModelAndView mav = new ModelAndView(ROLE_LIST);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page.setPageNo(pageNo);
		page = roleService.search(page, filters);
		mav.addObject("page", page);
		return mav;
	}
	
	@RequestMapping(value="/edit.htm",method=RequestMethod.GET)
	public ModelAndView edit(Long id,Integer pageNo)throws AppException{
		ModelAndView mav = new ModelAndView(ROLE_EDIT);
		if (id!=null) {
			Role role = roleService.get(id);
			mav.addObject("role", role);
		}
		mav.addObject("pageNo", pageNo);
		List<Authority> auths = authorityService.getAll();
		mav.addObject("auths", auths);
		return mav;
	}
	
	
	@RequestMapping(value="/modify.htm",method=RequestMethod.POST)
	public ModelAndView modify(Long id,HttpServletRequest request)throws AppException{
		
		try {
			String name = request.getParameter("name");
			String enabled = request.getParameter("enabled");
			if (name==null||enabled==null) {
				throw new AppException();
			}
			Role role = null;
			if (id==null) {
				role = new Role();	
			}else {
				role = roleService.get(id);
			}
			role.setName(name);
			role.setCreateTime(new Date());
			role.setEnabled(enabled.equals("1")?true:false);
			String[] aids = request.getParameterValues("auth");
			List<Long> ids = Lists.newArrayList();
			if (aids!=null) {
				for (String aid : aids) {
					if (aid!=null&&!"".equals(aid)) {
						ids.add(Long.parseLong(aid));
					}
				}
				List<Authority> auths = authorityService.get(ids);
				role.setAuthorities(auths);
			}else{
				role.setAuthorities(null);
			}
			roleService.add(role);
		} catch (Exception e) {
			e.printStackTrace();
			throw new AppException();
		}
		return   new ModelAndView(new RedirectView("list.htm?pageNo="+request.getParameter("pageNo")));
	}
	
	@RequestMapping(value="/edit.htm")
	public ModelAndView edit(Long id,String name,Date createTime,boolean enabled,Integer pageNo,HttpServletRequest request){
		Role role = roleService.get(id);
		ModelAndView mav = new ModelAndView(ROLE_EDIT);
		List<Authority> auths = authorityService.getAll();
		mav.addObject("auths", auths);	
		mav.addObject(role);
		return mav;
	}

	@ResponseBody
	@RequestMapping(value="/delete.htm")
	public String delete(String ids)throws AppException{
		try {
			if (ids!=null) {
				StringTokenizer st = new StringTokenizer(ids,"-");
				List<Long> list = Lists.newArrayList();
				while (st.hasMoreTokens()) {
					String id = st.nextToken();
					if (id!=null&&!"".equals(id)) {
						list.add(Long.parseLong(id));
					}
				}
				
				roleService.delete(list);
				return "删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "该角色正在使用，暂时不能删除！";
		}
		return "参数异常！";
		
	}
	
}
	
